﻿import QtQuick 2.11
//import QtQuick.Controls 1.4
import QtQuick.Controls 2.2
import QtGraphicalEffects 1.0

import ".."
import "../_Global"
import "../_Global/Button"

import "../_Global/Global.js" as GlobalJS
import "../GameJS.js" as GameJS

Rectangle {
    property alias mouseReg : mouseReg
    property alias mouseForgot : mouseForgot
    property alias mouseMinimized : mouseMinimized

    property alias buttonLogin: buttonLogin
    property alias labelMsg: labelMsg
    property alias textUser : textUser
    property alias textPass : textPass

    signal s_login()

    color: "#00000000"
    //width: 674
    //height: 394
    //anchors.fill: parent

    Image {
        id: root
        source: Global.resource.images.gameLogin.main
        anchors.fill: parent
/*
        //TextEdit透明
        TextField {
            id: textUser
            x: 151
            y: 117
            width: 372
            height: 43
            //color: "#ffffff"
            //autoScroll: false
            //cursorVisible: true
            font.pixelSize: 22
            focus: true
        }

        TextField {
            id: textPass
            x: 151
            y: 177
            width: 372
            height: 43
            //color: "#ffffff"
            autoScroll: false
            echoMode: TextInput.Password
            //cursorVisible: true
            //antialiasing: true
            font.pixelSize: 22
        }

        Image {
            id: inputNameImage
            x: 150
            y: 116
            width: 374
            height: 45
            source: Global._FixResourcePath_R("Media/Images/Login/Input.png")
        }
*/

        TextField {
            id: textUser
            x: Global.gamePos.mapLogin.rectTextUser.x
            y: Global.gamePos.mapLogin.rectTextUser.y
            width: Global.gamePos.mapLogin.rectTextUser.width
            height: Global.gamePos.mapLogin.rectTextUser.height
            //color: "#ffffff"
            //autoScroll: false
            //cursorVisible: true
            font.pixelSize: Global.gamePos.mapLogin.nFontSize
            //font.family: Global.resource.fonts.font1.name
            selectByMouse: true
            focus: true
            background: Rectangle {
                //color: control.enabled ? "transparent" : "#353637"
                //border.color: control.enabled ? "#21be2b" : "transparent"
                color: "transparent"
            }

            /*style: TextFieldStyle {
                textColor: "black"
                background: Rectangle {
                    color: "#00000000"
                    //radius: 2
                    //implicitWidth: 100
                    //implicitHeight: 24
                    //border.color: "#333"
                    //border.width: 1
                }
            }*/

        }

        TextField {
            id: textPass
            x: Global.gamePos.mapLogin.rectTextPass.x
            y: Global.gamePos.mapLogin.rectTextPass.y
            width: Global.gamePos.mapLogin.rectTextPass.width
            height: Global.gamePos.mapLogin.rectTextPass.height
            //color: "#ffffff"
            //autoScroll: false
            echoMode: TextInput.Password
            //cursorVisible: true
            //antialiasing: true
            font.pixelSize: Global.gamePos.mapLogin.nFontSize
            //font.family: Global.resource.fonts.font1.name
            selectByMouse: true
            background: Rectangle {
                //color: control.enabled ? "transparent" : "#353637"
                //border.color: control.enabled ? "#21be2b" : "transparent"
                color: "transparent"
            }
            /*style: TextFieldStyle {
                textColor: "black"
                background: Rectangle {
                    color: "#00000000"
                }
            }*/
        }

        ImageButton2 {
            id: buttonLogin
            x: Global.gamePos.mapLogin.rectLogin.x
            y: Global.gamePos.mapLogin.rectLogin.y
            width: Global.gamePos.mapLogin.rectLogin.width
            height: Global.gamePos.mapLogin.rectLogin.height
            source: Global.resource.images.gameLogin.login
            enabled: false
            onButtonClicked: {
                s_login();
            }
        }

        Image {
            id: forgotImage
            x: Global.gamePos.mapLogin.rectForgot.x
            y: Global.gamePos.mapLogin.rectForgot.y
            width: Global.gamePos.mapLogin.rectForgot.width
            height: Global.gamePos.mapLogin.rectForgot.height
            source: Global.resource.images.gameLogin.forgotPass
            MouseArea {
                id: mouseForgot
                anchors.fill: parent

                hoverEnabled: true
                onEntered: {
                    _private.showColorOverlay(forgotImage);
                    cursorShape = Qt.PointingHandCursor;
                }
                onExited: {
                    _private.hideColorOverlay();
                    cursorShape = Qt.ArrowCursor;
                }
            }
        }

        Image {
            id: regImage
            x: Global.gamePos.mapLogin.rectReg.x
            y: Global.gamePos.mapLogin.rectReg.y
            width: Global.gamePos.mapLogin.rectReg.width
            height: Global.gamePos.mapLogin.rectReg.height
            source: Global.resource.images.gameLogin.reg
            MouseArea {
                id: mouseReg
                anchors.fill: parent

                hoverEnabled: true
                onEntered: {
                    _private.showColorOverlay(regImage);
                    cursorShape = Qt.PointingHandCursor;
                }
                onExited: {
                    _private.hideColorOverlay();
                    cursorShape = Qt.ArrowCursor;
                }
            }
        }

        Image {
            id: imageRememberPass
            x: Global.gamePos.mapLogin.rectRememberPass.x
            y: Global.gamePos.mapLogin.rectRememberPass.y
            width: Global.gamePos.mapLogin.rectRememberPass.width
            height: Global.gamePos.mapLogin.rectRememberPass.height
            source: Global.resource.images.gameLogin.rememberPass
            MouseArea {
                id: mouseRemember
                anchors.fill: parent
                onClicked: {
                    rectRemember.checked = !rectRemember.checked;
                }

                hoverEnabled: true
                onEntered: {
                    _private.showColorOverlay(imageRememberPass);
                    cursorShape = Qt.PointingHandCursor;
                }
                onExited: {
                    _private.hideColorOverlay();
                    cursorShape = Qt.ArrowCursor;
                }
            }
        }

        Image {
            id: imageMin
            x: Global.dpW(600)
            y: Global.dpH(20)
            width: Global.dpW(20)
            height: Global.dpH(23)
            source: Global.resource.images.gameLogin.min
            visible: source != ""
            MouseArea {
                id: mouseMinimized
                anchors.fill: parent

                hoverEnabled: true
                onEntered: {
                    _private.showColorOverlay(imageMin);
                    cursorShape = Qt.PointingHandCursor;
                }
                onExited: {
                    _private.hideColorOverlay();
                    cursorShape = Qt.ArrowCursor;
                }
            }
        }

        Image {
            id: imageClose
            x: Global.dpW(625)
            y: Global.dpH(20)
            width: Global.dpW(20)
            height: Global.dpH(23)
            source: Global.resource.images.gameLogin.close
            visible: source != ""
            MouseArea {
                id: mouseClose
                anchors.fill: parent
                onClicked: {
                    Qt.quit();
                    console.debug("quit")
                }

                hoverEnabled: true
                onEntered: {
                    _private.showColorOverlay(imageClose);
                    cursorShape = Qt.PointingHandCursor;
                }
                onExited: {
                    _private.hideColorOverlay();
                    cursorShape = Qt.ArrowCursor;
                }
            }
        }

        /*
        L_Radio {
            id: rectRemember
            x: Global.dpW(294)
            y: Global.dpH(232)
            width: Global.dpW(17)
            height: Global.dpH(17)
            langrageSize: Global.dpH(17)
            //radius: 5
            //border.color: control.activeFocus ? "darkblue" : "gray"
            border.width: 1
            checked: GameManager.gameConfig["rememberPassword"]

            onCheckedChanged: {
                GameManager.sl_qml_SetGameConfig("rememberPassword", checked);
                //console.debug("状态:",checked, GameManager.gameConfig["rememberPassword"]);
                //console.debug("状态:",checked);
            }
        }*/

        //选择框
        Rectangle {
            property bool checked: GameManager.gameConfig["rememberPassword"]
            id: rectRemember
            x: Global.gamePos.mapLogin.rectRememberPassRect.x
            y: Global.gamePos.mapLogin.rectRememberPassRect.y
            width: Global.gamePos.mapLogin.rectRememberPassRect.width
            height: Global.gamePos.mapLogin.rectRememberPassRect.height
            //radius: 5
            //border.color: control.activeFocus ? "darkblue" : "gray"
            border.width: 1

            Rectangle {
                visible: rectRemember.checked
                color: "#555"
                border.color: "#333"
                //radius: 5
                anchors.margins: Global.dpW(4)
                anchors.fill: parent
            }
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    rectRemember.checked = !rectRemember.checked;
                }
            }

            onCheckedChanged: {
                GameManager.sl_qml_SetGameConfig("rememberPassword", checked);
                //console.debug("状态:",checked, GameManager.gameConfig["rememberPassword"]);
                //console.debug("状态:",checked);
            }
        }

        Label {
            id: labelMsg
            x: Global.gamePos.mapLogin.rectMessage.x
            y: Global.gamePos.mapLogin.rectMessage.y
            width: Global.gamePos.mapLogin.rectMessage.width
            height: Global.gamePos.mapLogin.rectMessage.height
            text: qsTr("正在检测新版本，请稍候...")
            font.pixelSize: Global.gamePos.mapLogin.nMessageFontSize
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }

        /*
        CheckBox {
            id: checkRemember
            x: Global.dpW(293)
            y: Global.dpH(227)
            //width: Global.dpW(100)
            //height: Global.dpH(100)
            checked: GameManager.gameConfig["rememberPassword"]
            style: CheckBoxStyle {

                indicator: Rectangle {
                        implicitWidth: Global.dpW(17)
                        implicitHeight: Global.dpH(17)
                        radius: 3
                        border.color: control.activeFocus ? "darkblue" : "gray"
                        border.width: 1

                        Rectangle {
                            visible: control.checked
                            color: "#555"
                            border.color: "#333"
                            radius: 1
                            anchors.margins: Global.dpW(5)
                            anchors.fill: parent
                        }
                }
                / *
                label: Label {
                    text: "记住密码"
                    font.pixelSize: Global.dpH(16)
                    color: "white"
                    font.family: ""
                }* /
            }
            onCheckedChanged: {
                GameManager.sl_qml_SetGameConfig("rememberPassword", checked);
                console.debug("状态:",checked, GameManager.gameConfig["rememberPassword"]);
            }
        }*/
    }

    QtObject {
        id: _private
        property ColorOverlay colorOverlay:
        //颜色遮罩(不可用时黑色,source必须和本对象同一层次!!!)
        ColorOverlay {
            //id: colorOverlay
            visible: false
            //anchors.fill: imageStudy;
            //source: imageStudy;
            color: "#40FFFFFF"
            parent: root
        }

        function showColorOverlay(image) {
            colorOverlay.anchors.fill = image
            colorOverlay.source = image
            colorOverlay.visible = true

        }
        function hideColorOverlay() {
            colorOverlay.visible = false
        }
    }

    Keys.onPressed:  {
        if(event.key == Qt.Key_Return
                || event.key == Qt.Key_Enter) {
            if(textUser.focus == true)
                textPass.focus = true;
            else
                s_login()
        }
        //console.debug("Key pressed!!!")
    }

    Component.onCompleted: {
        if(GameManager.gameConfig["LoginUserName"] != undefined) {
            textUser.text = GameManager.gameConfig["LoginUserName"];
        }
        if(GameManager.gameConfig["LoginUserPass"] != undefined) {
            textPass.text = GameManager.gameConfig["LoginUserPass"];
        }
    }

}
